package eteam.aps.controller;

import eteam.aps.api.U8Api;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Assert;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.LimitHelper;
import eteam.aps.model.entity.arrival.ArrivalRepository;
import eteam.aps.model.entity.arrival.ArrivalVouch;
import eteam.aps.model.entity.other.Goods;
import eteam.aps.model.entity.other.GoodsCurrent;
import eteam.aps.model.entity.other.Repository;
import eteam.aps.model.vo.arrival.ArrivalRepositoryBody;
import eteam.aps.model.vo.arrival.ArrivalRepositoryVo;
import eteam.aps.model.vo.arrival.ArrivalVouchVo;
import eteam.aps.service.ArrivalVouchService;
import eteam.aps.zn.ZNApiLimit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * @author LiuLei
 */
@Api(tags = "采购到货")
@RestController
@RequestMapping("/pag")
public class PurchasingArrivalGoodsController {

    @Autowired
    private ArrivalVouchService arrivalVouchService;

    @PostMapping("/gr")
    @ApiOperation("获取仓库信息")
    public R<List<Repository>> getRep() throws Exception {
        return R.success(U8Api.getRepository());
    }

    @PostMapping("/gd")
    @ApiOperation("获取仓库信息")
    public R<Goods> getGood(String barCode) throws Exception {
        if (Helper.isEmpty(barCode)) throw new BusinessException(HttpStatus.BAD_REQUEST, "条码不能为空！");
        return R.success(U8Api.getGoods(barCode));
    }

    @PostMapping("/sbc")
    @ApiOperation("根据到货单号获取本地到货单信息")
    @ApiImplicitParam(name = "bar", value = "到货单单号")
    public R<Set<ArrivalVouch>> selByArCode(String bar) {
        if (Helper.isEmpty(bar)) throw new BusinessException(HttpStatus.BAD_REQUEST, "条码号不能为空");
        Set<ArrivalVouch> arrivalVouch = arrivalVouchService.selPurByBar(bar);
        return R.success(arrivalVouch);
    }

    @PostMapping("/sbd")
    @ApiOperation("根据单据获取本地到货单信息")
    @ApiImplicitParam(name = "code", value = "到货单单据号")
    public R<Set<ArrivalVouchVo>> selByDCode(String code) {
        if (Helper.isEmpty(code)) throw new BusinessException(HttpStatus.BAD_REQUEST, "单据号不能为空");
        return R.success(arrivalVouchService.selPurByCode(Helper.filterStrEng(code)));
    }

    @PostMapping("/gsp")
    @ApiOperation("返回一张入库单")
    @ApiImplicitParam(value = "入库单信息", name = "repository")
    public R<ArrivalRepositoryVo> getRepository(@RequestBody ArrivalRepository repository) throws Exception {
        judgeArrivalHead(repository);
        return R.success(arrivalVouchService.getRepository(repository));
    }

    @GetMapping("/fccn")
    @ApiOperation("根据储位编号和名称查看库存是否存在")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "code", value = "储位编号"),
                    @ApiImplicitParam(name = "name", value = "储位名称")
            }
    )
    public R<Integer> findCurrentIsExistsByPositionCodeAndName(String code, String name) throws Exception {
        if (Helper.isEmpty(code)) {
            throw new BusinessException("储位编号不能为空！");
        }
        if (Helper.isEmpty(name)) {
            throw new BusinessException("储位名称不能为空！");
        }
        return R.success(arrivalVouchService.findCurrentIsExistsByPositionCodeAndName(code, name));
    }

    @PostMapping("/sp")
    @ApiOperation("生成一张U8入库单")
    @ApiImplicitParam(value = "入库单信息", name = "repository")
    public R<String> saveRepository(@RequestBody ArrivalRepositoryVo repository) throws Exception {
        try {
            judgeArrivalHead(repository.getRepository());
            LimitHelper.limit(repository.getRepository().getBarCode(), ZNApiLimit.ARRIVAL_SAVE_TIME, ZNApiLimit.ARRIVAL_SAVE_UNIT);
            List<ArrivalRepositoryBody> body = repository.getBody();
            for (ArrivalRepositoryBody repositoryBody : body) {
                Assert.isEmpty(repositoryBody.getCPosition(), "储位不能为空！");
                Assert.isEmpty(repositoryBody.getCInvCode(), "物料编码不能为空！");
                Assert.isEmpty(repositoryBody.getIQuantity(), "数量不能为空！");
                Assert.isTrue(repositoryBody.getIQuantity().compareTo(BigDecimal.ZERO) <= 0, "数量必须大于0！");
                // 取消批号判断
//                Assert.isEmpty(repositoryBody.getCBatch(), "批号不能为空！");
                Assert.isEmpty(repositoryBody.getDMDate(), "生产日期不能为空！");
                Assert.isEmpty(repositoryBody.getCVarvCode(), "到货单号不能为空！");
                if (!repositoryBody.getDMDate().isEmpty() && repositoryBody.getDMDate().length() > 10)
                    repositoryBody.setDMDate(repositoryBody.getDMDate().substring(0, 10));
            }
            String res = arrivalVouchService.saveRepository(repository);
            return R.success(res);
        } finally {
            // 执行成功就解除限制
            LimitHelper.removeLimit(repository.getRepository().getBarCode());
        }
    }

    private static void judgeArrivalHead(ArrivalRepository repository) {
        Assert.isEmpty(repository.getcWhCode(), "仓库编码不能为空！");
        Assert.isEmpty(repository.getcMaker(), "制单人不能为空！");
        Assert.isEmpty(repository.getcDepCode(), "部门编码不能为空！");
        Assert.isEmpty(repository.getcPersonCode(), "业务员编码不能为空！");
        Assert.isEmpty(repository.getBarCode(), "条码不能为空！");
    }

    @PostMapping("sgc")
    @ApiOperation("查询货架以及库存信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "存货编码", name = "code", required = true),
            @ApiImplicitParam(value = "仓库编号", name = "whCode", required = false),
    }
    )
    public R<List<GoodsCurrent>> selGoodsAndCurrent(String code, String whCode) {
        if (Helper.isEmpty(code)) throw new BusinessException(HttpStatus.BAD_REQUEST, "存货编码不能为空！");
        return R.success(U8Api.getPositionInfo(code, whCode));
    }


}
